package com.mck.service.impl;

import com.mck.anno.OperationLog;
import com.mck.entity.Notice;
import com.mck.mapper.NoticeMapper;
import com.mck.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    // 获取公告
    @Override
    @Cacheable(value = "notices", key = "'all'", unless = "#result.isEmpty()")  // 添加到缓存中
    public List<Notice> getNotice() {
        return noticeMapper.getNotice();
    }

    // 添加公告
    @OperationLog
    @Override
    @CacheEvict(value = "notices", key = "'all'")  // 删除缓存
    public void addNotice(Notice notice) {
        notice.setCreateTime(LocalDateTime.now());

        noticeMapper.addNotice(notice);
    }

    // 修改公告
    @OperationLog
    @Override
    @CacheEvict(value = "notices", key = "'all'")
    public void updateNotice(Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());

        noticeMapper.updateNotice(notice);
    }
}
